package gdal_grid;

import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
import utils.SelectFile;

import java.io.File;

public class Raster2Vector {
    public static void main(String[] args) {
        String pathTif = "E:\\Test\\result\\result2.tif";
        String pathShape = "E:\\Test\\test3.shp";

        raster2V(pathTif, pathShape);
    }

    public static DataSource dataSource = null;
    public static Layer dataLayer = null;

    /**
     * 矢量转栅格
     * @param hDataset 输入
     * @param pathShape 输出
     * @param driver
     */
    public static void raster2V(Dataset hDataset, String pathShape, Driver driver) {

        DataSource dataSource = driver.CreateDataSource(pathShape);
        SpatialReference spatialReference = new SpatialReference(hDataset.GetProjectionRef());
        System.out.println(hDataset.GetProjectionRef());


        Layer layer = dataSource.CreateLayer("data", spatialReference, ogrConstants.wkbPolygon);
        FieldDefn oFieldnew = new FieldDefn();
        oFieldnew.SetName("value");
        oFieldnew.SetType(ogr.OFTInteger);
        layer.CreateField(oFieldnew);

        Band band = hDataset.GetRasterBand(1);

        gdal.Polygonize(band, null, layer, 0);

        Double[] noData = new Double[1];
        band.GetNoDataValue(noData);
        long count0 = layer.GetFeatureCount();
        int v = layer.SetAttributeFilter("Value = " + noData[0]);
        long index = layer.GetFeatureCount();
        for (int i = 0; i < index; i++) {
            Feature feature = layer.GetNextFeature();
            long fid = feature.GetFID();
            layer.DeleteFeature(fid);
        }
        dataSource.ExecuteSQL("repack " + layer.GetName());
        hDataset.delete();

        synchronized (Raster2Vector.class) {
            if (dataLayer == null) {
                dataLayer = Raster2Vector.dataSource.CreateLayer("data", spatialReference, ogrConstants.wkbPolygon);
                FieldDefn oFieldnew2 = new FieldDefn();
                oFieldnew2.SetName("value");
                oFieldnew2.SetType(ogr.OFTInteger);
                dataLayer.CreateField(oFieldnew2);
            }
            layer.SetAttributeFilter("");
            for (int i=0;i<layer.GetFeatureCount();i++) {
                Feature feature = layer.GetNextFeature();
                dataLayer.CreateFeature(feature);
            }
            layer.delete();
            dataSource.delete();
        }

        Runtime.getRuntime().gc();
    }

    public static void raster2V(String pathTif, String pathShape) {
        Driver driver = SelectFile.driverInit();

        Dataset hDataset = gdal.Open(pathTif);
        DataSource dataSource = driver.CreateDataSource(pathShape);
        SpatialReference spatialReference = new SpatialReference(hDataset.GetProjectionRef());

        Layer layer = dataSource.CreateLayer("data", spatialReference, ogrConstants.wkbPolygon);
        FieldDefn oFieldnew = new FieldDefn();
        oFieldnew.SetName("value");
        oFieldnew.SetType(ogr.OFTInteger);
        layer.CreateField(oFieldnew);

        Band band = hDataset.GetRasterBand(1);

        gdal.Polygonize(band, null, layer, 0);

        Double[] noData = new Double[1];
        band.GetNoDataValue(noData);
        int v = layer.SetAttributeFilter("Value = " + noData[0]);
        long index = layer.GetFeatureCount();
        for (int i = 0; i < index; i++) {
            Feature feature = layer.GetNextFeature();
            long fid = feature.GetFID();
            layer.DeleteFeature(fid);
        }
        dataSource.ExecuteSQL("repack " + layer.GetName());
    }
}
